using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Web.Core.Providers;

namespace Web.Core.Objects
{
    public class Carts // Thu lam mot cau truc khac
    {
        public List<Cart> listCart = new List<Cart>();

        public Carts() { }

        public Carts(Cart c) // Lay tu DB
        {
            listCart = DataProvider.GetInstance().GetCart(c).listCart;
        }

        public void RemoveItem(int productID)
        {
            foreach (Cart c in this.listCart)
                if (c.ProductID == productID)
                {
                    this.listCart.Remove(c);
                    break;
                }
        }

        public void AddItemForVisitor(int productID)
        {
            this.listCart.Add(new Cart(0, productID, 1, 0));
        }

        public void UpdateItem(int cartID, int productID, int amount, int memID)
        {
            foreach (Cart c in this.listCart)
                if (c.ProductID == productID)
                {
                    c.MemberID = memID;
                    c.CartID = cartID;
                    c.ProductAmount = amount;
                    break;
                }
        }

        public static int InsertMemberItem(Cart c)
        {
            return DataProvider.GetInstance().InsertDeleteUpdateCart(c, GlobalStorage.Action.Insert);
        }

        public static int DeleteMemberItem(Cart c)
        {
            return DataProvider.GetInstance().InsertDeleteUpdateCart(c, GlobalStorage.Action.Delete);
        }

        public static int UpdateMemberItem(Cart c)
        {
            return DataProvider.GetInstance().InsertDeleteUpdateCart(c, GlobalStorage.Action.Update);
        }

        public void MergeCarts(Carts des)
        {
            foreach (Cart c in des.listCart) // Visitor
            {
                bool flag = true;

                foreach (Cart sc in this.listCart) // Members
                {
                    if (sc.ProductID == c.ProductID)
                    {
                        flag = false;
                        break;
                    }
                }

                if (flag == true)
                {
                    System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
                    c.MemberID = (session["Membership"] as Member).ID;
                    InsertMemberItem(c);
                }
            }
        }

        public static Cart SelectCart(int productID, int memberID)
        {
            Carts result = DataProvider.GetInstance().GetCart(new Cart(0, productID, 0, memberID));
            if (result != null)
                return result.listCart[0];
            return null;
        }

        public static Cart SelectCartByID(int cartID)
        {
            Carts result = DataProvider.GetInstance().GetCart(new Cart(cartID, 0, 0, 0));
            if (result != null)
                return result.listCart[0];
            return null;
        }

        public static Carts SelectCartsByMemberID(int memberID)
        {
            Carts result = DataProvider.GetInstance().GetCart(new Cart(-1, 0, 0, memberID));
            if (result != null)
                return result;
            return null;
        }  

        public static string GetSummaryProductsInCart(int cartID)
        {
            System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
            string result = "";
            Carts arr = DataProvider.GetInstance().GetCart(new Cart(cartID, 0, 0, 0));
            foreach (Cart c in arr.listCart)
                result += Products.GetProductByID((int)session["Language"], c.ProductID).Name + " x " + c.ProductAmount + "<br />";
            return result;
        }

        public static double GetTotalPriceOfCart(int cartID)
        {
            System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
            double result = 0;
            Carts arr = DataProvider.GetInstance().GetCart(new Cart(cartID, 0, 0, 0));
            foreach (Cart c in arr.listCart)
                result += Products.GetProductByID((int)session["Language"], c.ProductID).Price * c.ProductAmount;
            return result;
        }
    }
}
